package com.luocj.muke.demo.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.luocj.muke.demo.R;
import com.luocj.muke.demo.fragment.IndicatorFragment;
import com.luocj.muke.demo.view.CustomIndicator;

public class CustomIndicatorActivity extends AppCompatActivity {

    private static final String TAG = CustomIndicatorActivity.class.getSimpleName();
    private ViewPager vp;
    private String[] title = {"a", "b", "b"};
    private CustomIndicator indicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_indicator);
        initView();
    }

    private void initView() {
        vp = findViewById(R.id.vp);
        VpAdapter adapter = new VpAdapter(getSupportFragmentManager());
        vp.setAdapter(adapter);
        indicator = findViewById(R.id.indicator);
        for (int i = 0; i < indicator.getChildCount(); i++) {
            int position = i;
            indicator.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.tv1:
                            Toast.makeText(CustomIndicatorActivity.this, position + "", Toast.LENGTH_SHORT).show();
                            indicator.scroll2(position,0);
                            vp.setCurrentItem(position);
                            break;
                        case R.id.tv2:
                            Toast.makeText(CustomIndicatorActivity.this, position + "", Toast.LENGTH_SHORT).show();

                            indicator.scroll2(position,0);
                            vp.setCurrentItem(position);
                            break;
                        case R.id.tv3:
                            Toast.makeText(CustomIndicatorActivity.this, position + "", Toast.LENGTH_SHORT).show();
                            indicator.scroll2(position,0);
                            vp.setCurrentItem(position);

                            break;
                    }
                }
            });
        }

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                indicator.scroll2(position, positionOffset);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private class VpAdapter extends FragmentStatePagerAdapter {
        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            IndicatorFragment fragment = null;
            switch (position) {
                case 0:
                    fragment = IndicatorFragment.getInstance(title[position]);
                    break;
                case 1:
                    fragment = IndicatorFragment.getInstance(title[position]);
                    break;
                case 2:
                    fragment = IndicatorFragment.getInstance(title[position]);
                    break;
            }

            return fragment;
        }

        @Override
        public int getCount() {
            return title.length;
        }
    }
}
